# coding=utf-8
#


from iminer.ecs.models import IDC
from iminer.ecs.models import Region
from iminer.ecs.models import AZone
from iminer.ecs.models import ECS


def backend(filename):
    """ """
    idc = "huawei"

    with open(filename) as fp:
        for cur_line in fp.readlines()[1:]:
            #import ipdb;ipdb.set_trace()
            cur_line = cur_line.strip().split(",")

            """
            ['名称', 'ID', '区域', '可用分区', '状态', '规格名称', 'vCPUs', '内存(GiB)', '镜像', '操作系统类型', '私有IP地址', '弹性IP', '带宽', '带宽类型', '虚拟私有云 ID', '虚拟私有云', '创建时间', '标签', '安全组', '计费模式', '到期时间', '描述']
            """

            """
['ecs-dfbc-0423304', '35be11a0-29e4-42c0-9d5f-4bc08af071ec', '贵州', '可用区1', '运行中', 's3.4xlarge.2', '16', '"32\t"', '\tUbuntu22.04 64位', 'Linux', '192.168.0.224', '140.246.119.181', '1Mbit/s', '独享', '3d03085a-6bb2-40d7-ad72-32fa5d149af9', '\tvpc-default', '2024-04-23 16:35:43 GMT+08:00', '', 'newsafe\t', '包年/包月', '2024-05-23 16:36:29 GMT+08:00', '']
            """

            ecs_name = cur_line[0]
            ecs_id = cur_line[1]
            region = cur_line[2]
            azone = cur_line[3]
            status = cur_line[4]
            instance_type = cur_line[5]
            cpu = int(cur_line[6])
            #import ipdb;ipdb.set_trace()
            mem = int(eval(cur_line[7]).strip())
            image = cur_line[8].strip()
            system = cur_line[9]
            inner_ip = cur_line[10]
            pub_ip = cur_line[11]
            bandwidth = cur_line[12]
            btype = cur_line[13]
            region_id = cur_line[14]
            vpc = cur_line[15].strip()
            ctime = cur_line[16]
            fee_type = cur_line[19]
            expire_time = cur_line[20]

            btype = 1 if btype == "独享" else 2

            ctime = " ".join(ctime.split(" ")[:2])
            expire_time = " ".join(expire_time.split(" ")[:2])

            idc, _ = IDC.objects.get_or_create(name=idc)
            region, _ = Region.objects.get_or_create(idc=idc, name=region, rid=region_id)
            azone, _ = AZone.objects.get_or_create(name=azone, region=region)

            ECS.objects.get_or_create(idc=idc,
                                      name=ecs_name,
                                      instance_id=ecs_id,
                                      azone=azone,
                                      region=region,
                                      instance_type=instance_type,
                                      cpu=cpu,
                                      mem=mem,
                                      image=image,
                                      system=system,
                                      inner_ip=inner_ip,
                                      pub_ip=pub_ip,
                                      bandwidth=bandwidth,
                                      btype=btype,
                                      vpc=vpc,
                                      ctime=ctime,
                                      fee_type=fee_type,
                                      expire_time=expire_time)
